63ffa3
@@ -57,6 +57,7 @@
 import org.apache.hadoop.fs.permission.FsAction;
 import org.apache.hadoop.fs.permission.FsPermission;
 import org.apache.hadoop.hdfs.DFSClient;
+import org.apache.hadoop.hdfs.DFSConfigKeys;
 import org.apache.hadoop.hdfs.DistributedFileSystem;
 import org.apache.hadoop.hdfs.MiniDFSCluster;
 import org.apache.hadoop.hdfs.MiniDFSNNTopology;
@@ -1148,10 +1149,22 @@
public HdfsEncryptionShim(URI uri, Configuration conf) throws IOException {
       DistributedFileSystem dfs = (DistributedFileSystem)FileSystem.get(uri, conf);
 
       this.conf = conf;
-      this.keyProvider = dfs.getClient().getKeyProvider();
+      this.keyProvider = isEncryptionEnabled(dfs.getClient(), dfs.getConf()) ?
+          dfs.getClient().getKeyProvider() : null;
       this.hdfsAdmin = new HdfsAdmin(uri, conf);
     }
 
+    private boolean isEncryptionEnabled(DFSClient client, Configuration conf) {
+      try {
+        DFSClient.class.getMethod("isHDFSEncryptionEnabled");
+      } catch (NoSuchMethodException e) {
+        // the method is available since Hadoop-2.7.1
+        // if we run with an older Hadoop, check this ourselves
+        return !conf.getTrimmed(DFSConfigKeys.DFS_ENCRYPTION_KEY_PROVIDER_URI, "").isEmpty();
+      }
+      return client.isHDFSEncryptionEnabled();
+    }
+
     @Override
     public boolean isPathEncrypted(Path path) throws IOException {
       Path fullPath;
